package com.prs.crm.action.flow;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.flow.FlowNodeName;
import com.prs.crm.domain.flow.FlowNodePerson;
import com.prs.crm.domain.flow.WorkFlow;
import com.prs.crm.domain.flow.WorkFlowAndPerson;
import com.prs.crm.domain.flow.WorkFlowNode;
import com.prs.crm.domain.flow.WorkFlowType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.ShareEim;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.flow.FlowNodeNameService;
import com.prs.crm.service.flow.FlowNodePersonService;
import com.prs.crm.service.flow.WorkFlowAndPersonService;
import com.prs.crm.service.flow.WorkFlowNodeService;
import com.prs.crm.service.flow.WorkFlowService;
import com.prs.crm.service.flow.WorkFlowTypeService;
import com.prs.crm.service.person.ShareEimService;

public class WorkFlowAction extends BaseAction {

	private static final long serialVersionUID = 4246366195516897095L;

	private WorkFlowService workflowService;

	private WorkFlow workflow;

	private PaginationSupport pagination;

	private Collection<WorkFlowType> getFlowType;

	private WorkFlowTypeService workflowTypeService;

	private String xml;

	private String flowTypes;

	private FlowNodeName flowNodeName;

	private FlowNodePerson flowNodePerson;

	private Person[] flowpersons;//审签人员
	private String[] flowPersonNames;
	private String[] flowPersonIds;
	
	private FlowNodeNameService flowNodeNameService;

	private FlowNodePersonService flowNodePersonService;
	
	private List<WorkFlowNode>   workflownode;
	private WorkFlowNodeService workFlowNodeService;
	private List<WorkFlowType> workFlowType;
	private WorkFlowAndPerson workFlowAndPerson;
	private WorkFlowAndPersonService workFlowAndPersonService;
	private List<FlowNodePerson> flowNodePersons;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private Statement stmt1;
	private ResultSet rs1;
	private Statement stmt2;
	private ShareEimService shareEimService;
	private String ClassName;
	private String userName;
	private String userPass;
	private String url1;
	private String address;
	private String baseName;
	public ShareEimService getShareEimService() {
		return shareEimService;
	}

	public void setShareEimService(ShareEimService shareEimService) {
		this.shareEimService = shareEimService;
	}

	public List<FlowNodePerson> getFlowNodePersons() {
		return flowNodePersons;
	}

	public void setFlowNodePersons(List<FlowNodePerson> flowNodePersons) {
		this.flowNodePersons = flowNodePersons;
	}

	public String[] getFlowPersonNames() {
		return flowPersonNames;
	}

	public void setFlowPersonNames(String[] flowPersonNames) {
		this.flowPersonNames = flowPersonNames;
	}

	public String[] getFlowPersonIds() {
		return flowPersonIds;
	}

	public void setFlowPersonIds(String[] flowPersonIds) {
		this.flowPersonIds = flowPersonIds;
	}

	public WorkFlowAndPerson getWorkFlowAndPerson() {
		return workFlowAndPerson;
	}

	public void setWorkFlowAndPerson(WorkFlowAndPerson workFlowAndPerson) {
		this.workFlowAndPerson = workFlowAndPerson;
	}

	public WorkFlowAndPersonService getWorkFlowAndPersonService() {
		return workFlowAndPersonService;
	}

	public void setWorkFlowAndPersonService(
			WorkFlowAndPersonService workFlowAndPersonService) {
		this.workFlowAndPersonService = workFlowAndPersonService;
	}

	public List<WorkFlowType> getWorkFlowType() {
		return workFlowType;
	}

	public void setWorkFlowType(List<WorkFlowType> workFlowType) {
		this.workFlowType = workFlowType;
	}

	public WorkFlowNodeService getWorkFlowNodeService() {
		return workFlowNodeService;
	}

	public void setWorkFlowNodeService(WorkFlowNodeService workFlowNodeService) {
		this.workFlowNodeService = workFlowNodeService;
	}

	public void setWorkflowService(WorkFlowService workflowService) {
		this.workflowService = workflowService;
	}

	public WorkFlowService getWorkflowService() {
		return workflowService;
	}

	public void setWorkflow(WorkFlow workflow) {
		this.workflow = workflow;
	}

	public WorkFlow getWorkflow() {
		return workflow;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	@Action("listWorkFlow")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkFlow.class);
		this.setPagination(this.getWorkflowService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action(value = "saveWorkFlow", results = {
			@Result(name = "success", type = "redirect", location = "listWorkFlow"),
			@Result(name = "input", type = "redirect", location = "designWorkFlow?workflow=${workflow.id}") })
	public String save() throws SQLException {
		String result = SUCCESS;
		if (workflow == null || workflow.getId() == null) {
			result = INPUT;
		}
//			workflow.setType(workFlowType);
		this.getWorkflowService().save(workflow);
		return result;
	}

	@Action("editWorkFlow")
	public String edit() {
		workFlowType=this.getWorkflowTypeService().getAllWorkFlowType();
		return SUCCESS;
	}

	@Action(value = "removeWorkFlow", results = {
			@Result(name = "success", type = "redirect", location = "listWorkFlow"),
			@Result(name = "error", location = "/flow/editWorkFlow.ftl") })
	public String remove() {
		if (this.getWorkflowService().removeFlow(workflow)) {
			return SUCCESS;
		} else {
			this.addActionError("璇ユ祦绋嬪凡缁忚浣跨敤 ,涓嶈兘鍒犻櫎");
			return ERROR;
		}

	}

	@Action(value = "enableWorkFlow", results = { @Result(name = "success", type = "redirect", location = "listWorkFlow") })
	public String enable() {
		this.getWorkflowService().executeEnable(workflow);
		return SUCCESS;
	}

	@Action(value = "disableWorkFlow", results = { @Result(name = "success", type = "redirect", location = "listWorkFlow") })
	public String disable() {
		this.getWorkflowService().executeDisable(workflow);
		return SUCCESS;
	}

	@Action("designWorkFlow")
	public String design() {
		this.setXml(this.getWorkflowService().getDesginXml(workflow));
		return SUCCESS;
	}

	@Action("setExaminePerson")
	public String setExaminePerson()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(WorkFlowNode.class);
//		criteria.add(Restrictions.eq("flowID",workflow.getId()+""));
		criteria.add(Restrictions.eq("flow.id", workflow.getId()));
		this.setPagination(this.getWorkFlowNodeService().listByPage(criteria,
							getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("setPersonWorkFlow")
	public String setPersonWorkFlow()
	{
		workflownode=this.getFlowNodePersonService().getWorkFlowNode(workflow);
		flowNodePersons=this.getFlowNodePersonService().getFlowNodePerson(workflow);
		return SUCCESS;
	}

	@Action(value = "saveWorkFlowDesign", results = {
			@Result(name = "success", type = "redirect", location = "designWorkFlow?workflow=${workflow.id}"),
			@Result(name = "error", location = "/flow/designWorkFlow.ftl") })
	public String saveDesign() {
		try {
			this.getWorkflowService().saveDesgin(workflow, xml);
			return SUCCESS;
		} catch (InValidException e) {
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}
@Action(value = "saveFlowNodePeson", results = {
			@Result(name = "success", type = "redirect", location = "setExaminePerson?workflow=${workflow.getId()}"),
			@Result(name = "error", location = "setPersonWorkFlow") })
	public String saveFlowNodePeson(){
		workflownode=this.getWorkFlowNodeService().getWorkFlowNode(workflow.getId());
		for (int i = 0; i < workflownode.size(); i++) {
			FlowNodePerson flowNodePerson1=new FlowNodePerson();
			flowNodePerson1.setNodeName_id(workflownode.get(i).getId());
			flowNodePerson1.setWorkFlowNode(workflownode.get(i));
			flowNodePerson1.setState(0);
//			System.out.println(flowpersons[i]);
			System.out.println(flowPersonNames[i]);
			System.out.println(flowPersonIds[i]);
//			flowNodePerson1.setNodePersonName(flowpersons[i]);//存取审签人员
			flowNodePerson1.setNodePerson_names(flowPersonNames[i]);
			flowNodePerson1.setNodeperson_id(flowPersonIds[i]);
			flowNodePerson1.setWorkFlow(workflow);
			this.getFlowNodePersonService().save(flowNodePerson1);
		}
			

		return SUCCESS;
	}
	public Collection<WorkFlowType> getGetFlowType() {
		if (getFlowType == null) {
			getFlowType = new ArrayList<WorkFlowType>();
		}
		return getFlowType;
	}

	public void saveEimWorkflow(WorkFlow workFlow) throws SQLException{
		ShareEim se=this.getShareEimService().getCode("se01");
		if (se!=null) {
			ClassName=se.getClassName();
			userName=se.getUserName();
			userPass=se.getUserPass();
			address=se.getIp();
			baseName=se.getBaseName();
			url1="jdbc:mysql://"+address+":3306/"+baseName;
		}
		try {
			Class.forName(ClassName);
			con = DriverManager.getConnection(url1, userName, userPass);
			stmt = this.con.createStatement();
			stmt1 = this.con.createStatement();
			stmt2 = this.con.createStatement();
			rs=stmt.executeQuery("select * from flow_flow where name='"+workFlow.getName()+"'");
			if (!rs.next()) {
				rs1=stmt1.executeQuery("select * from flow_type where name='"+workFlow.getWorkFlowType().getName()+"'");
				while(rs1.next()){
					String ids=rs1.getString("id");
					String sql="insert into flow_flow(name,remark,state,workFlowType_id) values('"+workFlow.getName()+"','"+workFlow.getRemark()+"','"+workFlow.getState()+"','"+ids+"')";
					stmt2.executeUpdate(sql);
				}
				rs1.close();
				rs.close();
				con.close();
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public void setGetFlowType(Collection<WorkFlowType> getFlowType) {
		this.getFlowType = getFlowType;
	}

	public WorkFlowTypeService getWorkflowTypeService() {
		return workflowTypeService;
	}

	public void setWorkflowTypeService(WorkFlowTypeService workflowTypeService) {
		this.workflowTypeService = workflowTypeService;
	}

	public String getFlowTypes() {
		return flowTypes;
	}

	public void setFlowTypes(String flowTypes) {
		this.flowTypes = flowTypes;
	}

	public FlowNodeName getFlowNodeName() {
		return flowNodeName;
	}

	public void setFlowNodeName(FlowNodeName flowNodeName) {
		this.flowNodeName = flowNodeName;
	}

	public FlowNodePerson getFlowNodePerson() {
		return flowNodePerson;
	}

	public void setFlowNodePerson(FlowNodePerson flowNodePerson) {
		this.flowNodePerson = flowNodePerson;
	}

	public FlowNodeNameService getFlowNodeNameService() {
		return flowNodeNameService;
	}

	public void setFlowNodeNameService(FlowNodeNameService flowNodeNameService) {
		this.flowNodeNameService = flowNodeNameService;
	}

	public FlowNodePersonService getFlowNodePersonService() {
		return flowNodePersonService;
	}

	public void setFlowNodePersonService(
			FlowNodePersonService flowNodePersonService) {
		this.flowNodePersonService = flowNodePersonService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<WorkFlowNode> getWorkflownode() {
		return workflownode;
	}

	public void setWorkflownode(List<WorkFlowNode> workflownode) {
		this.workflownode = workflownode;
	}

	public Person[] getFlowpersons() {
		return flowpersons;
	}

	public void setFlowpersons(Person[] flowpersons) {
		this.flowpersons = flowpersons;
	}

}
